package com.ruoyi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.CustomerInfo;
import com.ruoyi.bizsys.domain.RuleIn;
import com.ruoyi.common.utils.Func;
import com.ruoyi.riskrule.RuleEngineService;
import com.ruoyi.riskrule.config.RaiseLimitParamsLoader;
import com.ruoyi.riskrule.domain.RuleNextDto;
import com.ruoyi.riskrule.service.IRiskCustomerService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/riskrule")
@Slf4j
public class RiskRuleController {
    @Autowired
    private RuleEngineService ruleEngineService;

    @Autowired
    private RaiseLimitParamsLoader raiseLimitParamsLoader;

    @Autowired
    private IRiskCustomerService customerService;

    @GetMapping("/raiseLimit")
    public JSONObject riskRuleRaiseLimit(String ruleType, String customerNo) {
        log.info("远程接收riskRuleRaiseLimit方法，参数为:ruleType = {}, customerNo = {}", ruleType, customerNo);
        JSONObject result = ruleEngineService.riskRuleRaiseLimit(ruleType, customerNo);
        log.info("远程接收riskRuleRaiseLimit方法，结果返回:{}", result);
        return result;
    }

    @PostMapping("/riskRuleRaiseLimits")
    public JSONObject riskRuleRaiseLimits(@RequestBody JSONObject jsonObject) {
        log.info("远程接收 riskRuleRaiseLimit 方法, 参数为 jsonObject:{}", jsonObject.toJSONString());

        String loanSerialNo = jsonObject.get("loanSerialNo").toString();
        String customerNo = jsonObject.get("customerNo").toString();
        RuleIn ruleIn = jsonObject.getObject("ruleIn", RuleIn.class);

        JSONObject result = ruleEngineService.riskRuleRaiseLimit(loanSerialNo, customerNo, ruleIn);
        log.info("远程接收 riskRuleRaiseLimit 方法, 结果返回:{}", result);
        return result;
    }


    @GetMapping("/riskRuleEngine")
    public JSONObject riskRuleEngine(String applyType, String serialNo) {
        log.info("远程接收 riskRuleEngine 方法，参数为: applyType = {},serialNo = {}", applyType, serialNo);
        JSONObject result = ruleEngineService.riskRuleEngine(applyType, serialNo);
        log.info("远程接收 riskRuleEngine 方法，结果返回:{}", result);
        return result;
    }

    //无调用
//    @GetMapping("/riskRuleNextStep2")
//    public JSONObject riskRuleNextStep2(RuleNextDto ruleNextDTO, String applyType, String serialNo, String token, String entryId) {
//        log.info("远程调用riskRuleNextStep2方法,参数为:{},{},{},{},{}",ruleNextDTO,applyType,serialNo,token,entryId);
//        return ruleEngineService.riskRuleNextStep2(ruleNextDTO, applyType, serialNo, token, entryId);
//    }

    @GetMapping("/initRuleInParam")
    public RuleIn initRuleInParam(String loanSerialNo, String customerNo) {
        log.info("远程接收initRuleInParam方法, 参数为: loanSerialNo = {}, customerNo = {}", loanSerialNo, customerNo);
        RuleIn result = raiseLimitParamsLoader.initRuleInParam(loanSerialNo, customerNo);
        log.info("远程接收 initRuleInParam方法, 结果返回 RuleIn:{}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/updateExpiryDate")
    public int updateExpiryDate(@RequestBody CustomerInfo customerInfo) {
        log.info("远程接收updateExpiryDate方法, 参数为 customerInfo:{}", JSON.toJSONString(customerInfo));
        int result = customerService.updateExpiryDate(customerInfo);
        log.info("远程接收updateExpiryDate方法, 结果返回:{}", result);
        return result;
    }
}
